// pages/cashService/third/unCheckList/unCheckList.wxml.js\
const { request } = require('../../../../utils/request');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listHttpFlag:true, //true可请求
    isEnd:false,
    bagList: [],
    stationArray:[],
    comArray:[],
    loginType:'',
    queryParams:{
      checkStatus:'8',
      companyId:1,
      stationId:'',
      self:false,
      pageNo:1,
      pageSize:20
    },
    cIndex:0,
    sIndex:0,
    lineIndex:0,
    lineArr:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      loginType: wx.getStorageSync('userInfo').loginType
    })
    wx.setNavigationBarTitle({
      title: '未清点箱包'
    })
    this.getCompany();
  },
   /**
   * 获取公司列表
   * self:true个人 false团队
   * status:
   */
  getCompany(){
    request('/smsk/outsource/company', 'GET',{self:false , status:1}).then(res => {
      console.log(res.data)
      if (res.data.data.length != 0) {
        this.getStations(res.data.data[0].id)
        this.setData({
          comArray:res.data.data,
          ["queryParams.companyId"]:res.data.data[0].id
        })
        this.fetchList();
      } else {
        console.log(res)
      }
    })
  },
  /**
   * 获取站点列表
   */
  getStations(cId){
    this.getStationIine(cId);
    request('/smsk/outsource/stations', 'GET',{companyId:cId}).then(res => {
      console.log(res.data)
      if (res.data.code == 0) {
        res.data.data.unshift({id:'',stationName:'全部'})
        console.log(res.data.data)
        this.setData({
          stationArray:res.data.data,
          sIndex:0,
          ['queryParams.stationId']:res.data.data[0].id
        })
      } else {
        console.log(res)
      }
    })
  },
  /**
   * 获取路线列表
   * @param {*} e 
   */
  getStationIine(cId,mode){
    request('/smsk/outsource/lines', 'GET',{companyId:cId}).then(res => {
      console.log(res.data)
      if (res.data.code == 0) {
        res.data.data.unshift({lineId:'',lineName:'全部'})
          this.setData({
            lineArr:res.data.data
          })
        }
    })
  },
  pagePlus(){
    if(this.data.listHttpFlag === false){
      return;
    }
    this.setData({
      ['queryParams.pageNo'] : this.data.queryParams.pageNo + 1
    })
    this.fetchList();
  },
  fetchList(){
    if(this.data.listHttpFlag === false){
      return;
    }
    this.data.listHttpFlag = false;
    request('/smsk/outsource/list', 'GET', this.data.queryParams).then(res => {
      this.data.listHttpFlag = true;
      // console.log(res.data)
      if (res.data.code == 0) {
        let tempList = this.data.bagList
        res.data.data.forEach(item => {
          tempList.push(item)
        })
        console.log('qqqq',this.data.queryParams.pageNo * this.data.queryParams.pageSize,res.data.total)
        if((this.data.queryParams.pageNo * this.data.queryParams.pageSize) >= res.data.total){
          this.setData({
            listHttpFlag: false
          })
          if(tempList.length > 0){
            this.setData({
              isEnd: true
            })
          }
        }
        this.setData({
          bagList:tempList
        })
        // todo 做一个列表结束的标记
      } else {
        console.log(res)
      }
    })
  },
  /**
   * 工作台选择公司
   * @param {*} e 
   */
  companyPick(e){
    console.log(e)
    this.setData({
      bagList:[],
      cIndex:e.detail.value,
      ['queryParams.companyId']:this.data.comArray[e.detail.value].id,
      ['queryParams.pageNo']:1,
      listHttpFlag: true,
      isEnd: false
    })
    this.fetchList();
    this.getStations(this.data.comArray[e.detail.value].id);
  },
  /**
   * 工作台选择站点
   * @param {*} e 
   */
  stationPick(e){
    this.setData({
      bagList:[],
      sIndex:e.detail.value,
      ['queryParams.stationId']:this.data.stationArray[e.detail.value].id,
      ['queryParams.pageNo']:1,
      listHttpFlag: true,
      isEnd: false
    })
    this.fetchList();
  },
  /**
   * 跳转清点页
   */
  toCheck(e){
    console.log(e.target.dataset.item);
    let bagInfo = JSON.stringify(e.target.dataset.item)
    wx.navigateTo({
      url: '/pages/cashService/third/doCount/doCount?bagInfo=' + bagInfo,
    })
  }
})